package com.technology.education.ui.common.login

import android.os.Bundle
import com.technology.education.R
import com.technology.education.data.AppData
import com.technology.education.data.SpKeys
import com.technology.education.databinding.ActivityLoginBinding
import com.technology.education.ext.*
import com.technology.education.ui.common.information.CompleteInfoActivity
import com.technology.education.ui.common.register.ActivityRegister
import com.technology.education.ui.market.MainMarketActivity
import com.technology.education.ui.student.MainStudentActivity
import com.technology.education.ui.teacher.MainTeacherActivity
import com.technology.education.utils.rom.SettingsCompat
import me.hgj.jetpackmvvm.base.activity.BaseVmDbActivity
import me.hgj.jetpackmvvm.ext.parseState
import me.hgj.jetpackmvvm.ext.view.clickNoRepeat
import me.hgj.jetpackmvvm.util.startActivity

/**
 *@author:create by BZY
 *Date: 2022/2/24 09
 */
class ActivityLogin : BaseVmDbActivity<LoginViewModel, ActivityLoginBinding>() {
    override fun layoutId(): Int = R.layout.activity_login

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.vm = mViewModel
        mDatabind.toolBar.tvTitle.text = "登录"
        mDatabind.tvRegister.text =
            getMiddleColor("没有账号?去", "注册", "", getColor(R.color.login_selected))
        mDatabind.tvRegister.clickNoRepeat {
            if (AppData.MEMBER_TYPE == "2") {
                startActivity(com.technology.education.ui.market.register.ActivityRegister::class)
            } else {
                startActivity(ActivityRegister::class, Pair("type", 0))
            }
        }
        mDatabind.tvForget.clickNoRepeat {
            startActivity(ActivityRegister::class, Pair("type", 1))
        }
       mDatabind.toolBar.toolbar.setNavigationOnClickListener {
           onBackPressed()
       }
    }

    override fun showLoading(message: String) {
        showLoadingExt(message)
    }

    override fun dismissLoading() {
        dismissLoadingExt()
    }

    override fun createObserver() {
        mViewModel.loginResult.observe(this)
        {
            parseState(it, onSuccess = { info ->
                AppData.TOKEN = info.token
                saveData(SpKeys.TOKEN, info.token)
                saveData(SpKeys.MEMBER_TYPE, AppData.MEMBER_TYPE)
                if (info.name == null) {
                    this.startActivity(CompleteInfoActivity::class)
                } else {
                    saveData(SpKeys.IS_LOGIN, true)
                    when (AppData.MEMBER_TYPE) {
                        "0" -> {
                            getAppViewModel().getInfo()
                            this.startActivity(MainStudentActivity::class)
                        }
                        "1" -> this.startActivity(MainTeacherActivity::class)
                        "2" -> this.startActivity(MainMarketActivity::class)
                    }
                }
                onBackPressed()
            }, onError = { ex ->
                toast(ex.errorMsg)
            })

        }
    }

}